<?php
class BaseAction extends Action
{
    function _initialize()
    {
        $this->loadDefaultConfig();
    }

    /**
     * 错误信息利用语言包显示
     * @param string $name
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function errorLang($name)
    {
        $this->error(L($name));
        exit;
    }

    /**
     * 正确信息利用语言包显示
     * @param string $name
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function successLang($name)
    {
        $this->success(L($name));
        exit;
    }

    /**
     * 加载默认参数
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function loadDefaultConfig()
    {
        $this->assign('web_title', '摩客网 - 公共投票管理平台');
    }

    /**
     * 利用D进行操作验证
     * @param $modelName
     * @param null $data
     * @param bool $isAdd
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function createData($modelName, $data = '', $isAdd = true)
    {
        $Model = D($modelName); // 实例化User对象
        $data = $Model->create($data);
        $rs = false;
        if ($data) {
            if ($isAdd) {
                $rs = $Model->add($data);
            } else {
                $rs = $Model->save($data);
            }
            if ($rs) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $this->error($Model->getError());
            exit;
        }
    }

    /**
     * 获取分页信息
     * @param Int $count 总条数
     * @author Ice iceinto@mallog.com.cn
     * @return class Page
     */
    protected function getPage($count, $pageSize = 20)
    {
        return new Page($count, $pageSize);
    }

    /**
     * 邮件发送
     * @param string/array $users
     * @param array $title
     * @param array $body
     * @param striong $dir
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function sendMail($users, $title, $body, $dir)
    {
        Vendor('PHPMailer.SendMail');
        $mail = new SendMail();
        $mailDir = APP_PATH . '/Mail/' . $dir;
        $ViewTitle = Think::instance('View');
        $ViewBody = Think::instance('View');
        $ViewTitle->assign($title);
        $ViewBody->assign($body);
        $title = $ViewTitle->fetch($mailDir . '/title.html');
        $body = $ViewBody->fetch($mailDir . '/body.html');
        return $mail->setParameAndSend($title, $body, $users);
    }
}

?>